import os
import re
import sys

if __name__ == '__main__':
    print('即将格式化文件名中的数字，数字前补0')
    d = input("请输入格式化重命名文件名的目录：")
    if d is None or d == '':
        sys.exit()
    pat = '\d*'
    file_list = os.listdir(d)
    print(file_list)
    for f in file_list:
        ss = f.split('.')
        suffix = ss.pop()
        f_name = '.'.join(ss)
        r = re.compile(pat, re.S).findall(f_name)
        for i in r:
            if i != '':
                f_name = f_name.replace(i, i.zfill(3))
                # print(f_name + '.' + suffix)
                src = d + '/' + f
                dis = d + '/' + f_name + '.' + suffix
                print(src, dis)
                os.rename(src, dis)
                break
